import { Component, Injector, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { CreateOrUpdateSeoInput, EntityDtoOfGuid, MenuAndIdInputOfGuid, SeoDto, SeoServiceProxy } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap/modal';
import { finalize } from 'rxjs/operators';


@Component({
    selector: 'seo-modal',
    templateUrl: './seo-modal.component.html'
})
export class SeoModalComponent extends AppComponentBase {

    @ViewChild('modal', { static: true }) modal: ModalDirective;

    seoData:SeoDto = new SeoDto();
    saving = false;
    relatedId:string;
    menuId: number;

    constructor(
        injector: Injector,
        private _seoServiceProxy:SeoServiceProxy
    ) {
        super(injector);
    }

    show(relatedId: string, menuid: number, defaultTitle?: string){
        this.seoData = new SeoDto();
        this.saving = false;
        this.relatedId = relatedId;
        this.menuId = menuid;
        this.getSeoData(relatedId, menuid, defaultTitle);
    }


    getSeoData(relatedId:string, menuid: number,defaultTitle?:string){
        this._seoServiceProxy.getSeo(relatedId,defaultTitle,'','',true).subscribe(result => {
            this.seoData = result;
            this.modal.show();
        });
    }

    removeSeo(){
        this._seoServiceProxy.delet(new MenuAndIdInputOfGuid({
            id:this.seoData.id,
            menuId : this.menuId
        })).subscribe(result => {
            this.notify.success(this.l('ResetSuccessfully'));
            this.close();
        });
    }

    save(){
        this.saving = true;
        this._seoServiceProxy.createOrUpdateSeoSetting(new CreateOrUpdateSeoInput({
            relatedId:this.relatedId,
            menuId: this.menuId,
            title:this.seoData.title,
            description:this.seoData.description,
            keyWords:this.seoData.keyWords,
            customData:this.seoData.customData
        })).pipe(finalize(() => { this.saving = false; })).subscribe(result => {
            this.notify.success(this.l('SavedSuccessfully'));
            this.close();
        })
    }

    close(): void {
        this.modal.hide();
    }

}
